//
//  HBDiscoverItemRankListCell.m
//  HeartBeat
//
//  Created by karong on 2023/8/24.
//

#import "HBDiscoverItemRankListCell.h"
#import "HBDiscoverItemNovel2Cell.h"
#import "HBChannelItemModel.h"
#import "UIButton+ImageAlign.h"

@interface HBDiscoverItemRankListCell ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (weak, nonatomic) IBOutlet UIView *rankView;
@property (weak, nonatomic) IBOutlet UIView *滑塊;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *滑塊左邊距;
@property (weak, nonatomic) IBOutlet UIButton *推薦按鈕;
@property (strong, nonatomic) NSArray *推薦數組;
@property (strong, nonatomic) NSArray *人氣數組;
@property (strong, nonatomic) NSArray *完結數組;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) HBChannelItemModel * object;
@end

@implementation HBDiscoverItemRankListCell
- (void)awakeFromNib {
    [super awakeFromNib];
    UINib * nib = [UINib nibWithNibName:@"HBDiscoverItemNovel2Cell" bundle:nil];
    [self.listView registerNib:nib forCellWithReuseIdentifier:@"HBDiscoverItemNovel2Cell"];
    [self.moreBtn setButtonImageTitleStyle:(WSButtonImageTitleStyleRight) padding:4];
}

- (void)setDataWithObject:(HBChannelItemModel *)object {
    self.object = object;
    self.推薦數組 = object.rank_list.most_favors;
    self.人氣數組 = object.rank_list.most_read;
    self.完結數組 = object.rank_list.newest;
    self.titleLabel.text = object.name;
    self.moreBtn.hidden = !object.is_more;
    self.listView.width = self.bounds.size.width;
    self.listView.height = self.bounds.size.height;
    CGFloat width = 52;
    CGFloat height = width * 4 / 3.0;
    self.itemSize = CGSizeMake(HBScreenWidth - 12 - 12, height);
    [self.listView reloadData];
}
- (IBAction)btnAction:(UIButton *)sender {
    self.selectIndex = sender.tag;
    [self.listView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:sender.tag] atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:YES];
}

- (IBAction)moreAction:(id)sender {
    [HBEvenManager event:@"view_more" evenLogType:(HBEvenLogType_Click)
              attributes:@{@"Module_id":SF(@"%ld", (long)self.object.id)}];
}

#pragma mark - UICollectionViewDelete

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section == 0) {
        count = self.推薦數組.count;
    }
    else if (section == 1) {
        count = self.人氣數組.count;
    }
    else if (section == 2) {
        count = self.完結數組.count;
    }
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBDiscoverItemNovel2Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemNovel2Cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [cell setDataWithObject:self.推薦數組[indexPath.row] index:indexPath.row];
    }
    else if (indexPath.section == 1) {
        [cell setDataWithObject:self.人氣數組[indexPath.row] index:indexPath.row];
    }
    else if (indexPath.section == 2) {
        [cell setDataWithObject:self.完結數組[indexPath.row] index:indexPath.row];
    }
    return cell;
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x / scrollView.width;
    self.selectIndex = x;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.itemSize;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(discoverItemRankListCellDidSelectWithObject:)]) {
        if (indexPath.section == 0) {
            [self.delegate discoverItemRankListCellDidSelectWithObject:self.推薦數組[indexPath.row]];
        }
        else if (indexPath.section == 1) {
            [self.delegate discoverItemRankListCellDidSelectWithObject:self.人氣數組[indexPath.row]];
        }
        else if (indexPath.section == 2) {
            [self.delegate discoverItemRankListCellDidSelectWithObject:self.完結數組[indexPath.row]];
        }
    }
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    self.滑塊左邊距.constant = 2 + self.推薦按鈕.width * selectIndex;
    [self.rankView uploadLayout];
}
@end
